 package scripting;

 import client.MapleClient;
 import configs.ServerConfig;
 import constants.ServerConstants;
 import handling.netty.MapleCustomEncryption;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 import javax.script.Invocable;
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import scripting.item.ItemScriptManager;
 import scripting.npc.NPCScriptManager;
 import scripting.quest.QuestScriptManager;
 import tools.MaplePacketCreator;

 public abstract class AbstractScriptManager
 {
/*  28 */   private static final Logger log = LogManager.getLogger(AbstractScriptManager.class.getName());




/*  33 */   private final ScriptEngineManager sem = new ScriptEngineManager();


   protected Invocable getDefaultInvocable(String path) {
/*  37 */     try (InputStreamReader inputStreamReader = new InputStreamReader(getClass().getResourceAsStream(path))) {
/*  38 */       ScriptEngine engine = this.sem.getEngineByName("nashorn");
       try {
/*  40 */         engine.eval(inputStreamReader);
/*  41 */       } catch (ScriptException e) {
/*  42 */         e.printStackTrace();
       }
/*  44 */       return (Invocable)engine;
/*  45 */     } catch (IOException e) {
/*  46 */       log.error("加载默认脚本出错", e);

/*  48 */       return null;
     }
   }
   protected Invocable getHandlerInvocable(String path) {
/*  52 */     String filename = ServerConfig.WORLD_SCRIPTSPATH + "/" + path;
/*  53 */     File file = new File(filename);
/*  54 */     if (!file.exists()) {
/*  55 */       return null;
     }

/*  58 */     try(FileInputStream stream = new FileInputStream(file); 
/*  59 */         InputStreamReader inputStreamReader = new InputStreamReader(stream, "UTF-8")) {
/*  60 */       ScriptEngine engine = this.sem.getEngineByName("nashorn");
/*  61 */       engine.eval(inputStreamReader);
/*  62 */       return (Invocable)engine;
     }
/*  64 */     catch (IOException|ScriptException e) {
/*  65 */       log.error("加载系统脚本出错", e);

/*  67 */       return null;
     }
   }
   protected Invocable getInvocable(String path, MapleClient c) {
/*  71 */     return getInvocable(path, c, false);
   }

   protected Invocable getInvocable(String path, MapleClient c, boolean npc) {
/*  75 */     FileInputStream scriptFile_in = null;
/*  76 */     ByteArrayOutputStream out = null;
     try {
/*  78 */       path = ServerConfig.WORLD_SCRIPTSPATH + "/" + path;
/*  79 */       ScriptEngine engine = null;
/*  80 */       if (c != null) {
/*  81 */         engine = c.getScriptEngine(path);
       }
/*  83 */       if (engine == null) {
/*  84 */         File scriptFile = new File(path);
/*  85 */         if (!scriptFile.exists()) {
/*  86 */           return null;
         }
/*  88 */         engine = this.sem.getEngineByName("nashorn");
/*  89 */         if (c != null) {
/*  90 */           c.setScriptEngine(path, engine);
         }
/*  92 */         scriptFile_in = new FileInputStream(scriptFile);
/*  93 */         int fileLength = scriptFile_in.available();
/*  94 */         out = new ByteArrayOutputStream();
/*  95 */         byte[] buffer = new byte[fileLength];

/*  97 */         scriptFile_in.read(buffer);
/*  98 */         out.write(buffer);













/* 112 */         String script = "";
/* 113 */         if (buffer.length > 0) {

/* 115 */           boolean isEncryption = (buffer[0] == 35);
/* 116 */           if (isEncryption) {
/* 117 */             List<String> authList = new LinkedList<>();
/* 118 */             byte[] decrypt = Arrays.copyOfRange(buffer, 1, fileLength);
/* 119 */             MapleCustomEncryption.decryptData(decrypt);
/* 120 */             script = new String(decrypt, "UTF-8");
/* 121 */             String keystr = script.substring(0, script.indexOf("#"));
/* 122 */             authList.addAll(Arrays.asList(keystr.split("\\|")));
/* 123 */             boolean check = false;
/* 124 */             for (String key : authList) {
/* 125 */               if (key.contains(ServerConstants.服务器授权)) {
/* 126 */                 check = true;
               }
             }
/* 129 */             script = script.substring(keystr.length() + 1, script.length() - 1);
/* 130 */             if (!check && c != null && c.getPlayer() != null) {
/* 131 */               c.getPlayer().dropMessage(1, "该NPC未授权本服使用，请联系管理员(GM)。");
/* 132 */               c.removeClickedNPC();
/* 133 */               NPCScriptManager.getInstance().dispose(c);
/* 134 */               ItemScriptManager.getInstance().dispose(c);
/* 135 */               QuestScriptManager.getInstance().dispose(c);
/* 136 */               c.announce(MaplePacketCreator.enableActions());
/* 137 */               return null;
             }
           } else {
/* 140 */             script = new String(buffer, "UTF-8");
           }
         }

/* 144 */         engine.eval(script);
       }
/* 146 */       else if (npc) {
/* 147 */         c.getPlayer().dropMessage(-1, "您当前已经和1个NPC对话了. 如果不是请输入 @ea 命令进行解卡。");
       }
/* 149 */       return (Invocable)engine;
/* 150 */     } catch (Exception e) {
/* 151 */       log.error("Error executing script. Path: " + path + "\r\nException " + e);
/* 152 */       return null;
     } finally {
       try {
/* 155 */         if (scriptFile_in != null) {
/* 156 */           scriptFile_in.close();
         }
/* 158 */         if (out != null) {
/* 159 */           out.close();
         }
/* 161 */       } catch (IOException ignore) {
/* 162 */         log.error("Error close script. Path: " + path + "\r\nException " + ignore);
       }
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\scripting\AbstractScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */